<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
     */

    use AuthenticatesUsers {AuthenticatesUsers::login as trait_login;}

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    // 重写trait中的退出方法,退出后重定向
    public function loggedOut(Request $request)
    {
        return redirect('/login');
    }

    public function login(Request $request)
    {
        // $request会合并所有谓词请求
        if ($request->has('ugrant')) {
            return $this->loginUgrant($request);
        }
        return $this->trait_login($request);
    }

    // 授权测试，用取别名的方式解决使用Trait规则因优先级产生的问题
    protected function loginUgrant(Request $request)
    {
        $request->validate([
            'email' => 'required|string',
            'password' => 'required|string',
        ]);

        $http = new Client();
        // 发送相关字段到后端应用获取授权令牌
        $response = $http->post('http://ugrant.test/oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('services.ugrant_passport.appid'),
                'client_secret' => config('services.ugrant_passport.secret'),
                'username' => $request->input('email'), // 这里传递的是邮箱
                'password' => $request->input('password'), // 传递密码信息
                'scope' => '*',
            ],
        ]);

        return response($response->getBody());
    }

    // 请求获取授权码
    public function oauth()
    {
        $query = http_build_query([
            'response_type' => 'code',
            'client_id' => config('services.ugrant_passport.appid'),
            'redirect_uri' => config('services.ugrant_passport.callback'),
            'scope' => 'basic-user-info',
        ]);

        // 重定向到授权服务器，获取授权码;授权方生成授权码后会将其返回到回调地址
        return redirect('http://ugrant.test/oauth/authorize?' . $query);
    }

    // 使用授权码获取令牌
    public function callback(Request $request)
    {
        $code = $request->get('code');
        if (!$code) {
            dd('授权失败');
        }
        $http = new Client();
        $response = $http->post('http://ugrant.test/oauth/token', [
            'form_params' => [
                'grant_type' => 'authorization_code',
                'client_id' => config('services.ugrant_passport.appid'),
                'client_secret' => config('services.ugrant_passport.secret'),
                'redirect_uri' => config('services.ugrant_passport.callback'),
                'code' => $code,
            ],
        ]);

        return response($response->getBody());
    }

    //客户端授权令牌
    public function client()
    {
        $http = new Client();
        $response = $http->post('http://ugrant.test/oauth/token', [
            'form_params' => [
                'grant_type' => 'client_credentials',
                'client_id' => config('services.ugrant_passport.appid'),
                'client_secret' => config('services.ugrant_passport.secret'),
                'scope' => '*',
            ],
        ]);

        return response($response->getBody());
    }

    /**
     * 隐式授权令牌
     */
    public function implicit()
    {
        $callback = 'http://blog57.test/auth/implicit/callback';
        // 构造查询字符串
        $query = http_build_query([
            'client_id' => config('services.ugrant_passport.appid'),
            'redirect_uri' => $callback,
            'response_type' => 'token',
            'scope' => '',
        ]);

        return redirect('http://ugrant.test/oauth/authorize?' . $query);
    }

    public function implicitCallback(Request $request)
    {
        dd($request->get('access_token'));
    }
}
